# -*- coding: utf-8 -*-
from mod.client import extraClientApi

from legendaryPowerClientScript.ui.base.uiBase import UIBase
from legendaryPowerClientScript.util import apiUtil
from templateScript import templateClientSystem


# UI类，一定要继承UIBase
class ShootHud(UIBase):
    # 下面这两行别动
    def __init__(self, namespace, name, param):
        UIBase.__init__(self, namespace, name, param)
        print "Init ShootHud"

    def Create(self):
        # 在Create中才能正常获取到UI元素，构建界面
        # 获取根节点下叫 shoot 的按钮，并为它绑定 onShoot 函数。 它会在按钮点击抬起时触发
        print "Create ShootHud"
        self.shootButton = self.getButton("/shoot")
        self.shootButton.SetTouchUp(self.onShoot)

    def onShoot(self, args):
        # args是一个dict，其中键值对的说明： https://mc.163.com/dev/mcmanual/mc-dev/mcdocs/1-ModAPI/%E6%8E%A5%E5%8F%A3/%E8%87%AA%E5%AE%9A%E4%B9%89UI/UI%E6%8E%A7%E4%BB%B6.html#setbuttontouchdowncallback

        #向服务端发送一个包，让服务端生成一支箭并发射出去
        templateClientSystem.mainSys.NotifyToServer("ClientShoot", {
            "playerId": extraClientApi.GetLocalPlayerId()
        })
        # 将发射按钮变成暗色，过几秒后恢复
        self.shootButton.getImage("default").SetSpriteColor((0.7, 0.7, 0.7))
        self.shootButton.SetTouchEnable(False)
        apiUtil.GetScheduler().addUI(self.resetShootButton, 20)

    def resetShootButton(self):
        self.shootButton.getImage("default").SetSpriteColor((1, 1, 1))
        self.shootButton.SetTouchEnable(True)
